package com.ruoyi.project.system.task.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.task.domain.Task;
import com.ruoyi.project.system.task.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/task")
public class TaskController extends BaseController {

    private String prefix = "system/task";

    @Autowired
    private TaskService taskService;

    /**
     * 跳转到任务管理
     * @return
     */
    @RequiresPermissions("system:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 跳转到日程
     * @return
     */
    @RequiresPermissions("system:task:view")
    @GetMapping("schedule")
    public String role()
    {
        return prefix + "/schedule";
    }


    /**
     * 新增任务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 修改任务
     */
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("task", taskService.selectTaskById(taskId));
        return prefix + "/edit";
    }

    /**
     * 新增保存任务
     */
    @RequiresPermissions("system:task:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Task task)
    {
        System.out.println(task.toString());
        return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改保存公告
     */
    @RequiresPermissions("system:task:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Task task)
    {
        System.out.println(task.toString());
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 根据任务id删除任务
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(taskService.deleteTaskByIds(ids));
    }

    /**
     * 更具条件分页查询
     * @param task 条件
     * @return
     */
    @RequiresPermissions("system:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        System.out.println(list.get(0).toString());
        return getDataTable(list);
    }

    /**
     * 一件导出表
     * @param task 查询条件
     * @return
     */
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:task:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        return util.exportExcel(list, "任务数据");
    }

}
